﻿
namespace System
{
    /// <summary>
    /// 提供CLR基本类型的安全的转换操作
    /// </summary>
    public static class TypeConverter
    {
        /// <summary>
        /// 安全的转换为Int类型,转换失败返回给定的默认值
        /// </summary>
        public static int ToSafeInt(this object source, int defaultVal = -1)
        {
            if (source == null) return defaultVal;
            string temp = source.ToString();
            int val;
            return int.TryParse(temp, out val) ? val : defaultVal;
        }

        public static decimal ToSafeDecimal(this object source, decimal defaultVal = 0)
        {
            if (source == null) return defaultVal;
            string temp = source.ToString();
            decimal val;
            return decimal.TryParse(temp, out val) ? val : defaultVal;
        }

        public static short ToSafeShort(this object source, short defaultVal = 0)
        {
            if (source == null) return defaultVal;
            string temp = source.ToString();
            short val;
            return short.TryParse(temp, out val) ? val : defaultVal;
        }

        public static DateTime ToSafeDateTime(this object source)
        {
            return ToSafeDateTime(source, DateTime.MinValue);
        }

        public static DateTime ToSafeDateTime(this object source, DateTime defalutVal)
        {
            if (source == null) return defalutVal;
            string temp = source.ToString();
            DateTime val;
            return DateTime.TryParse(temp, out val) ? val : defalutVal;
        }

        public static string ToDateString(this DateTime source, DateFormat format = DateFormat.LongDateWithLine)
        {
            string val = string.Empty;
            switch (format)
            {
                case DateFormat.ShortDateWithLine:
                    val = source.ToString("yyyy-MM-dd");
                    break;
                case DateFormat.LongDateWithLine:
                    val = source.ToString("yyyy-MM-dd HH:mm:ss");
                    break;
                case DateFormat.ShortDateWithSlash:
                    val = source.ToString("yyyy/MM/dd");
                    break;
                case DateFormat.LoneDateWithSlash:
                    val = source.ToString("yyyy/MM/dd HH:mm:ss");
                    break;
                case DateFormat.ChineseShortDate:
                    val = source.ToString("yyyy年MM月dd日");
                    break;
                case DateFormat.ChineseLongDate:
                    val = source.ToString("yyyy年MM月dd日 hh时mm分ss秒");
                    break;
                case DateFormat.ChineseShortDateWithoutDay:
                    val = source.ToString("yyyy年MM月");
                    break;
                case DateFormat.ChineseShortDateWithoutYear:
                    val = source.ToString("MM月dd日");
                    break;
            }

            return val;
        }
    }

    /// <summary>
    /// 时间格式
    /// </summary>
    public enum DateFormat
    {
        /// <summary>
        /// yyyy-MM-dd
        /// </summary>
        ShortDateWithLine,

        /// <summary>
        /// yyyy-MM-dd HH:mm:ss
        /// </summary>
        LongDateWithLine,

        /// <summary>
        /// yyyy/MM/dd
        /// </summary>
        ShortDateWithSlash,

        /// <summary>
        /// yyyy/MM/dd HH:mm:ss
        /// </summary>
        LoneDateWithSlash,

        /// <summary>
        /// yyyy年MM月dd日
        /// </summary>
        ChineseShortDate,

        /// <summary>
        /// yyyy年MM月dd日 hh时mm分ss秒
        /// </summary>
        ChineseLongDate,

        /// <summary>
        /// yyyy年MM月
        /// </summary>
        ChineseShortDateWithoutDay,

        /// <summary>
        /// MM月dd日
        /// </summary>
        ChineseShortDateWithoutYear
    }
}
